﻿using System;
using System.Data;
using System.Linq;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;


namespace ZX.WebApp.WorkFlow
{
    public partial class AccreditWorkflow : System.Web.UI.Page
    {
        string UserId = "";
        //调用流程服务接口实例
        CrmWorkFlowService.WorkFlowServiceClient workFlowServiceClient = null;
        public DataTable WorkFlowTable; //流程数据源
        public DataTable WorkTaskTable; //节点数据源
        public DataTable Opercontent; //处理者信息数据源
        public DataTable Operator; //操作者数据源
        public string  CalssUer = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            //实例化流程服务接口
            workFlowServiceClient = new CrmWorkFlowService.WorkFlowServiceClient();
            UserId = this.Page.User.Identity.Name;
            //BaseUserPage.LoadWaitPage();
            if (!this.Page.IsPostBack)
            {
               
                WorkFlowTable = workFlowServiceClient.GetWorkFlow(); //流程列表
                WorkTaskTable = workFlowServiceClient.GetWorkTasksView(); //节点列表
                Opercontent = workFlowServiceClient.GetOpercontent(UserId); //处理者列表
                Operator = workFlowServiceClient.TaskOperator();  //操作者列表
                foreach (DataRow item in Opercontent.Rows)
                {
                    if (CalssUer == "")
                    {
                        CalssUer = "'"+item["OperContent"].ToString()+"'";
                    }
                    else
                    {
                        CalssUer += "," + "'" + item["OperContent"].ToString() + "'";
                    }
                }
                LoadLeftTree("####", UserId, tvWorkflow.Nodes);
                bind();
            }
        }
        private void bind()
        {
            DataTable acDt = workFlowServiceClient.GetToAccreditTable(UserId);
            gvAccredit.DataSource = acDt;
            gvAccredit.DataBind();

            acDt = workFlowServiceClient.GetHaveAccreditTable(UserId);
            gvHaveAccredit.DataSource = acDt;
            gvHaveAccredit.DataBind();

        }
        /// <summary>
        /// 递归装载任务导航
        /// </summary>
        /// <param name="key"></param>
        /// <param name="startNodes"></param>
        public void LoadLeftTree(string key, string userId, TreeNodeCollection startNodes)
        {
            try
            {
                if (key == null || key == "") key = "####";
                DataTable table = workFlowServiceClient.GetChildClass(key);
                
                foreach (DataRow row in table.Rows)
                {
                    TreeNode tmpNode = new TreeNode();
                    tmpNode.Text = row["Caption"].ToString();
                    tmpNode.Target = "_self";
                    tmpNode.ImageUrl = @"..\Images\WorkFlow\tree_27.gif";
                    tmpNode.NavigateUrl = "#";
                    tmpNode.Value = row["WfclassId"].ToString();
                    startNodes.Add(tmpNode);
                    LoadWorkflow(tmpNode.Value, tmpNode.ChildNodes);
                    LoadLeftTree(tmpNode.Value, userId, tmpNode.ChildNodes);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("LoadLeftTree函数," + ex.Message);
            }

        }
        public void LoadWorkflow(string key, TreeNodeCollection startNodes)
        {
           // DataTable table = workFlowServiceClient.GetWorkFlowsByClassId(key);
            string classId = string.Empty; 
            DataRow[] table = WorkFlowTable.Select("WFClassId = '" + key + "'");
            foreach (DataRow row in table)
            {
                TreeNode tmpNode = new TreeNode();
                tmpNode.Text = row["FlowCaption"].ToString();
                tmpNode.ImageUrl = @"..\Images\WorkFlow\workflow.ico";
                tmpNode.NavigateUrl = "#";
                tmpNode.Value = row["WorkFlowId"].ToString();
                startNodes.Add(tmpNode);
                LoadWorkTask(tmpNode.Value, tmpNode.ChildNodes);
            }
          
        }
        public void LoadWorkTask(string key, TreeNodeCollection startNodes)
        {
            DataRow[] rowes = WorkTaskTable.Select("WorkFlowId = '" + key + "'");
            string tasktypeId = "";
            foreach (DataRow row in rowes)
            {
                tasktypeId = row["tasktypeid"].ToString();
               // if (tasktypeId == "4" || tasktypeId == "6" ) continue;//控制节点和子流程节点
                if (tasktypeId == "1")
                {
                    //if (Operator.Select("WorkFlowid='" + key + "' and WorkTaskid='" + row["WorkTaskId"].ToString() + "' and  tasktypeId = '1' and (OperContent IN (" + CalssUer + ") or OperContent='ALL')").Length > 0)
                    //{
                        TreeNode tmpNode = new TreeNode();
                        tmpNode.Text = row["TaskCaption"].ToString();
                        tmpNode.ImageUrl = @"..\Images\WorkFlow\worktask.ico";
                        tmpNode.NavigateUrl = "#";
                        tmpNode.Value = row["WorkTaskId"].ToString();
                        tmpNode.ShowCheckBox = true;
                        startNodes.Add(tmpNode);
                    //}
                }
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            if (tvWorkflow.CheckedNodes.Count <= 0)
            {
                //WorkDialog.ShowMessage(this, "请先选择授权的任务!");
                return;
            }
            if (CustomId.Value.Length <= 0)
            {
                //WorkDialog.ShowMessage(this, "请先选择授权的用户!");
                return;
            }
            
            string[] users = CustomId.Value.Split(',');
            
            foreach (TreeNode tn in tvWorkflow.CheckedNodes)
            {
                foreach (string touser in users)
                {
                    if (UserId == touser.Trim()) continue;//自己给自己授权无效

                    workFlowServiceClient.AssignAccredit(UserId, touser.Trim(), tn.Parent.Value, tn.Value);

                }
            }
            bind();
            Label1.Text = "授权成功!";
        }


        protected void btnTackAccredit_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gvr in gvAccredit.Rows)
            {
                CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
                if (ch.Checked)
                {
                    workFlowServiceClient.RemoveMoveAccredits(gvAccredit.DataKeys[gvr.DataItemIndex].Value.ToString());
                }
            }
            bind();
        }
    }
    
}